package com.zhentao.service.impl;

import com.alibaba.fastjson.JSON;
import com.zhentao.api.article.ArticleClientApi;
import com.zhentao.mapper.WmChannelMapper;
import com.zhentao.mapper.WmNewsMapper;
import com.zhentao.mapper.WmUserMapper;
import com.zhentao.model.article.dto.ArticleDto;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.wemwdia.pojo.WmChannel;
import com.zhentao.model.wemwdia.pojo.WmNews;
import com.zhentao.model.wemwdia.pojo.WmUser;
import com.zhentao.service.WmNewsAutoScanService;
import com.zhentao.service.WmNewsTaskService;
import com.zhentao.utils.aliyun.TextModerationPlusUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class WmNewsAutoScanServiceImpl implements WmNewsAutoScanService {
    @Autowired
    private WmNewsMapper wmNewsMapper;
    @Autowired
    private WmChannelMapper wmChannelMapper;
    @Autowired
    private ArticleClientApi articleClientApi;
    @Autowired
    private WmUserMapper wmUserMapper;
    @Autowired
    private WmNewsTaskService wmNewsTaskService;

    @Override
    public void autoScanWnMews(Integer id) {
        WmNews wmNews = wmNewsMapper.selectById(id);

        if(wmNews!=null){
//            获取内容
            String title = wmNews.getTitle();

            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append(title);
            List<Map> maps = JSON.parseArray(wmNews.getContent(), Map.class);
            for (Map map : maps) {
//                传过来的是一个图片
                if(map.get("type").equals("text")){
//                    把获取到的图片保存到images里面 -list集合
                    stringBuilder.append((String) map.get("value"));
                }
            }
            ResponseResult result= TextModerationPlusUtil.checkContent(stringBuilder.toString());
            if(result.getCode()==600){ //审核通过
                wmNews.setStatus((short)8);
                wmNewsMapper.updateById(wmNews);
                //审核通过之后--》需要同步信息
                ArticleDto articleDto=new ArticleDto();
                WmChannel wmChannel = wmChannelMapper.selectById(wmNews.getChannelId());
                if(wmChannel !=null){
                    articleDto.setChannelName(wmChannel.getName());
                    articleDto.setChannelId(wmChannel.getId());
                }
                WmUser wmUser = wmUserMapper.selectById(wmNews.getUserId());
                articleDto.setAuthorName(wmUser.getName());
                articleDto.setAuthorId(wmNews.getUserId().longValue());
                articleDto.setContent(wmNews.getContent());
                articleDto.setTitle(wmNews.getTitle());
                articleDto.setImages(wmNews.getImages());

                ResponseResult responseResult = articleClientApi.saveArticle(articleDto);
                if(responseResult.getCode() == 200){
//                    进行关联
                    wmNews.setArticleId((Long) responseResult.getData());
                    Date publishTime = wmNews.getPublishTime();
                    if(!publishTime.after(new Date())){
                        wmNews.setStatus((short) 9);
                        wmNewsMapper.updateById(wmNews);
                    }
                }
            }else if(result.getCode()==602){
//                转人工
                wmNews.setStatus((short)3);
                wmNewsMapper.updateById(wmNews);
            }else {
                wmNews.setStatus((short)2);
                wmNews.setReason(result.getErrorMessage());
                wmNewsMapper.updateById(wmNews);
            }
        }
    }
}
